package exercise;

/**
 * @author shhjiang_001
 * @create 2020-07-08 23:17
 */
public class QuickSortTest {
    public static void main(String[] args) {
        int[] arr = new int[]{3,4,1,2,6,9,8,5,7};
        quickSort(arr,0,arr.length-1);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
    }

    private static void quickSort(int[] arr, int left, int right) {
        if (left > right) return;
        int base = arr[left];
        int i = left;
        int j = right;

        while (i != j){
            // 从右往左，再从左往右
            while (arr[j] >= base && i < j){
                j--;
            }
            while (arr[i] <= base && i< j){
                i++;
            }
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        arr[left] = arr[i];
        arr[i] = base;
        quickSort(arr,left,i-1);
        quickSort(arr,i+1,right);
    }
}
